#!/bin/bash

# 中文文档：https://flume.liyifeng.org/

source /home/shell/util.sh

# 安装函数
function install () {
    requireJava
    source /etc/profile.d/java.sh
    
    if [ ! -d "/opt/flume/" ]; then
        version=`curl -sL https://mirrors.tuna.tsinghua.edu.cn/apache/flume/stable|grep -Po "apache-flume-.*?-bin.tar.gz"|uniq|perl -pe "s/apache-flume-(.*)-bin.tar.gz/\1/g"`;
        download /opt/flume.tar.gz https://mirrors.tuna.tsinghua.edu.cn/apache/flume/stable/apache-flume-${version}-bin.tar.gz
        tar -xf /opt/flume.tar.gz -C /opt/
        rm -rf /opt/flume.tar.gz
        mv /opt/apache-flume-1.9.0-bin /opt/flume
    fi
}

# 配置函数
function config(){
    cp -r /opt/flume/conf/flume-conf.properties.template /opt/flume/conf/flume-conf.properties
    cp -r /opt/flume/conf/flume-env.sh.template /opt/flume/conf/flume-env.sh

    read -p "agent名[myagent]：" agent_name
    agent_name=${agent_name:-"myagent"}

tee /opt/flume/conf/flume-env.sh -a<<EOF
export JAVA_HOME=/opt/jdk/java

export JAVA_OPTS="-Xms100m -Xmx2000m -Dcom.sun.management.jmxremote"
export JAVA_OPTS="\$JAVA_OPTS -Dorg.apache.flume.log.rawdata=true -Dorg.apache.flume.log.printconfig=true "

EOF

}

function runagent () {
    # /opt/flume/bin/flume-ng agent -n $agent_name --conf conf --conf-file /opt/flume/conf/flume-conf.properties
    echo "run"
}

# 运行安装和配置
install && config